package com.mysiteforme.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.mysiteforme.admin.base.BaseController;
import com.mysiteforme.admin.entity.BlogChannel;
import com.mysiteforme.admin.entity.BlogTags;
import com.mysiteforme.admin.entity.VO.ZtreeVO;
import com.xiaoleilu.hutool.date.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.mysiteforme.admin.entity.News;
import com.mysiteforme.admin.service.NewsService;
import com.baomidou.mybatisplus.plugins.Page;
import com.mysiteforme.admin.util.LayerData;
import com.mysiteforme.admin.util.RestResponse;
import com.mysiteforme.admin.annotation.SysLog;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.ServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 新闻  前端控制器
 * </p>
 *
 * @author wangl
 * @since 2018-05-16
 */

@Controller
@Api(value = "demo演示", description = "主要提供演示测试相关的接口API")
@RequestMapping("/admin/news")
public class NewsController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(NewsController.class);

    @Autowired
    private NewsService newsService;

    @GetMapping("list")
    @SysLog("跳转新闻列表")
    public String list(){
        return "/admin/news/list";
    }

    @ApiOperation(value="获取所有用户详细信息", notes="获取用户列表信息")
    @PostMapping("list")
    @ResponseBody
    @SysLog("请求新闻列表数据")
    public LayerData<News> list(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                      @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                                      ServletRequest request){
        Map map = WebUtils.getParametersStartingWith(request, "s_");
        LayerData<News> layerData = new LayerData<>();
        EntityWrapper<News> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag",false);
        if(!map.isEmpty()){
            String title = (String) map.get("title");
            if(StringUtils.isNotBlank(title)) {
                wrapper.like("title",title);
            }else{
                map.remove("title");
            }

            String anthor = (String) map.get("anthor");
            if(StringUtils.isNotBlank(anthor)) {
                wrapper.like("anthor",anthor);
            }else{
                map.remove("anthor");
            }

        }
        Page<News> pageData = newsService.selectDetailArticle(map,new Page<>(page,limit));

        layerData.setData(pageData.getRecords());
        layerData.setCount(pageData.getTotal());
        return layerData;
    }

    @GetMapping("add")
    @SysLog("跳转新增新闻页面")
    public String add(@RequestParam(value = "channelId",required = false)Long channelId, Model model){
        BlogChannel blogChannel = blogChannelService.selectById(channelId);
        if(blogChannel != null){
            model.addAttribute("channel",blogChannel);
        }
        List<ZtreeVO> list = blogChannelService.selectZtreeData();
        model.addAttribute("ztreeData", JSONObject.toJSONString(list));
        List<BlogTags> blogTags = blogTagsService.listAll();
        model.addAttribute("taglist",blogTags);
        return "/admin/news/add";
    }

    @ApiOperation(value="创建用户", notes="根据User对象创建用户")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "java.lang.Long", name = "id", value = "id", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "News", name = "News", value = "新闻信息", required = true)
    })
    @PostMapping("add")
    @SysLog("保存新增新闻数据")
    @ResponseBody
    public RestResponse add(@RequestBody News news){
        if(StringUtils.isBlank(news.getTitle())){
            return RestResponse.failure("标题不能为空");
        }
        if(news.getchannelId() == null){
            return RestResponse.failure("栏目ID不能为空");
        }
        newsService.insert(news);
        return RestResponse.success();
    }

    @GetMapping("edit")
    @SysLog("跳转编辑新闻页面")
    public String edit(Long id,Model model){
        News news = newsService.selectById(id);
        model.addAttribute("news",news);
        return "/admin/news/edit";
    }

    @PostMapping("edit")
    @ResponseBody
    @SysLog("保存编辑新闻数据")
    public RestResponse edit(News news){
        if(null == news.getId() || 0 == news.getId()){
            return RestResponse.failure("ID不能为空");
        }
        if(StringUtils.isBlank(news.getTitle())){
            return RestResponse.failure("标题不能为空");
        }
        newsService.updateById(news);
        return RestResponse.success();
    }

    @PostMapping("delete")
    @ResponseBody
    @SysLog("删除新闻数据")
    public RestResponse delete(@RequestParam(value = "id",required = false)Long id){
        if(null == id || 0 == id){
            return RestResponse.failure("ID不能为空");
        }
        News news = newsService.selectById(id);
        news.setDelFlag(true);
        newsService.updateById(news);
        return RestResponse.success();
    }

}